package cn.wgx.module.file.service.imp;

import cn.wgx.commons.storage.fastdfs.FastDFSClient;
import cn.wgx.module.file.entity.FileInfo;
import cn.wgx.module.file.entity.FileType;
import cn.wgx.module.file.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.Serializable;

@Slf4j
@Service
public class FastDFSServiceImpl extends FileService {


    @Override
    protected void doUploadFile(MultipartFile file, FileInfo fileInfo) {

        try {
            String s = FastDFSClient.uploadFile(file);
            String url = FastDFSClient.getFileWebServerUrl() + s;
            fileInfo.setUrl(url);
            fileInfo.setDisPath(url);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected boolean doDelete(Serializable id) {
        FileInfo fileInfo = getById(id);
        if(fileInfo != null){
            FastDFSClient.deleteFile(fileInfo.getUrl());
        }
        return true;
    }

    @Override
    public FileType getFileType() {
        return FileType.FASTDFS;
    }


}
